/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.basicdata.basictree.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.service.TreeService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.basicdata.basictree.entity.PartDeviceNode;
import com.jeeplus.modules.basicdata.basictree.mapper.PartDeviceNodeMapper;

/**
 * 设备节点Service
 *
 * @author 李宏博
 * @version 2020-09-28
 */
@Service
@Transactional(readOnly = true)
public class PartDeviceNodeService extends TreeService<PartDeviceNodeMapper, PartDeviceNode> {

    public PartDeviceNode get(String id) {
        return super.get(id);
    }

    public List<PartDeviceNode> findList(PartDeviceNode partDeviceNode) {
        if (StringUtils.isNotBlank(partDeviceNode.getParentIds())) {
            partDeviceNode.setParentIds("," + partDeviceNode.getParentIds() + ",");
        }
        return super.findList(partDeviceNode);
    }

    @Transactional(readOnly = false)
    public void save(PartDeviceNode partDeviceNode) {
        super.save(partDeviceNode);
    }

    @Transactional(readOnly = false)
    public void delete(PartDeviceNode partDeviceNode) {
        super.delete(partDeviceNode);
    }

    public String codeGet(String code) {
        return mapper.codeGet(code);
    }

    public String codeReturn(String code, String level) {
		if(code.length() == 5){
			return code;
		}
        String codeName = mapper.codeReturn(code, level);
        if (codeName != null) {
                if (codeName.length() == 2) {
                    int a = Integer.parseInt(codeName);
                    a++;
                    if (a < 10) {
                        codeName = "0" + a;
                    } else {
                        codeName = String.valueOf(a);
                    }
                }
                if (codeName.length() == 5) {
                    int a = Integer.parseInt(codeName);
                    a++;
                    if (a < 10000) {
                        codeName = "0" + a;
                    } else {
                        codeName = String.valueOf(a);
                    }
                }
                if (codeName.length() >= 9) {
                    char[] charCode = codeName.toCharArray();
                    int x = charCode[charCode.length - 3] - 48;
                    int y = charCode[charCode.length - 2] - 48;
                    int z = charCode[charCode.length - 1] - 48;
                    int va = 100 * x + 10 * y + z + 1;
                    x = va / 100;
                    y = (va - x * 100) / 10;
                    z = va - x * 100 - y * 10;
                    charCode[charCode.length - 3] = (char) (x + 48);
                    charCode[charCode.length - 2] = (char) (y + 48);
                    charCode[charCode.length - 1] = (char) (z + 48);
                    codeName = new String(charCode);
                }
        }
        return codeName;
    }
}